package com.yita.demo01_base;

import java.sql.*;

public class Demo06_SelectByFun {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        // 1.加载驱动程序
        Class.forName("com.mysql.cj.jdbc.Driver");

        String url = "jdbc:mysql://localhost:13306/fruitdb";
        String user = "root";
        String pwd = "root";
        //2.获取数据库连接
        Connection conn = DriverManager.getConnection(url,user,pwd);

        String sql = "select count(*) from t_fruit;";

        //3.创建PreparedStatement对象
        PreparedStatement preparedStatement = conn.prepareStatement(sql);


        //4.执行sql语句
        ResultSet resultSet = preparedStatement.executeQuery();

        //5.处理结果集
        while (resultSet.next()) {
            int count = resultSet.getInt(1);
            System.out.println("总记录数：" + count);
        }

        resultSet.close();
        preparedStatement.close();
        conn.close();
    }
}
